package nacos

import (
	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"
	"github.com/spf13/viper"
	"log"
)

// 创建Nacos Client
func getConfigClient() (config_client.IConfigClient, error) {
	return clients.NewConfigClient(initNacos())
}

// 获取Nacos Config
func GetConfig() (string, error) {
	if viper.GetBool("debug") {
		log.Printf("viper读取nacos注册服务 :::data-id: %s,group: %s", viper.GetString("data-id"), viper.GetString("group"))
	}

	cl, err := getConfigClient()
	if err != nil {
		return "", err
	}
	content, err := cl.GetConfig(vo.ConfigParam{
		DataId: viper.GetString("data-id"),
		Group:  viper.GetString("group"),
	})
	return content, err
}
